//using System;
//using System.Collections.Generic;
//using System.Text;
//using System.Collections;
//using Microsoft.SqlServer.Dts.Runtime;
//using Microsoft.SqlServer.Dts.Tasks.ScriptTask;
//using System.Runtime.InteropServices;

//namespace SsisUtilities.PacMan.Components
//{
//    [System.Guid("2EC4E475-058D-3590-8909-279F833D8B9C"), ComVisible(true), DtsTask(LocalizationType = typeof(Localized), TaskType = "DTS90", UITypeName = "Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptTaskUI, Microsoft.SqlServer.ScriptTaskUI, Version=9.00.242.00, Culture=Neutral, PublicKeyToken=89845dcd8080cc91")]
//    public class ScriptTask : Task, IDTSBreakpointSite, IDTSSuspend, IDTSComponentPersist
//    {
//        internal Project ActiveProject;
//        private string[] LOG_EVENT_NAMES = new string[] { "ScriptTaskLogEntry" };
//        private static Hashtable m_assemblyCache = new Hashtable();
//        private const string m_attrColumn = "Column";
//        private const string m_attrCondition = "Condition";
//        private const string m_attrConditionType = "ConditionType";
//        private const string m_attrEnabled = "Enabled";
//        private const string m_attrEntryPoint = "EntryPoint";
//        private const string m_attrFile = "File";
//        private const string m_attrFunction = "Function";
//        private const string m_attrHitCount = "HitCount";
//        private const string m_attrHitCountType = "HitCountType";
//        private const string m_attrLanguage = "Language";
//        private const string m_attrLine = "Line";
//        private const string m_attrName = "Name";
//        private const string m_attrReadOnlyVariables = "ReadOnlyVariables";
//        private const string m_attrReadWriteVariables = "ReadWriteVariables";
//        private const string m_attrSaveBinaries = "SaveBinaries";
//        private BreakpointManager m_bpManager;
//        private ArrayList m_breakpointList = new ArrayList();
//        private ScriptTaskCodeProvider m_codeProvider = new ScriptTaskCodeProvider();
//        private int m_debugMode = 0;
//        private const string m_elemBinaryItem = "BinaryItem";
//        private const string m_elemBreakpoint = "Breakpoint";
//        private const string m_elemProjectItem = "ProjectItem";
//        private const string m_elemScriptProject = "ScriptProject";
//        private object m_executionValue;
//        internal const string m_monikerRoot = "dts://Scripts/";
//        private string m_readOnlyVariables;
//        private string m_readWriteVariables;
//        private string m_referencePath;
//        private bool m_saveBinaries;
//        private string m_scriptEntryPoint;
//        private string m_scriptProject;
//        public ScriptLanguageInfo ScriptLanguageInfo;

//        public ScriptTask()
//        {
//            string location = typeof(Package).Assembly.Location;
//            string str2 = Assembly.GetExecutingAssembly().Location;
//            this.m_referencePath = location.Substring(0, location.LastIndexOf('\\')) + ";" + str2.Substring(0, str2.LastIndexOf('\\'));
//            string sqlPath = GetSqlPath();
//            if (sqlPath != null)
//            {
//                this.m_referencePath = this.m_referencePath + ";" + sqlPath + @"\SDK\Assemblies";
//            }
//            this.Initialize();
//        }

//        public void AcceptBreakpointManager(BreakpointManager bpManager)
//        {
//            this.m_bpManager = bpManager;
//        }

//        public override DTSExecResult Execute(Connections connections, VariableDispenser variableDispenser, IDTSComponentEvents events, IDTSLogging log, object transaction)
//        {
//            this.m_executionValue = null;
//            try
//            {
//                if (!this.ScriptLoaded)
//                {
//                    return DTSExecResult.Success;
//                }
//                if (events.FireQueryCancel())
//                {
//                    return DTSExecResult.Canceled;
//                }
//                ScriptObjectModel scriptObjectModel = new ScriptObjectModel(connections, variableDispenser, events, log, transaction, this.m_readOnlyVariables, this.m_readWriteVariables);
//                string.Format("{0}.{1}", this.m_scriptProject, this.m_scriptEntryPoint);
//                ScriptRuntime runtime = null;
//                Assembly engineAssembly = null;
//                if ((this.m_saveBinaries && this.CodeProvider.HasBinaryCode) && ((this.m_breakpointList.Count == 0) || !this.DebugMode))
//                {
//                    try
//                    {
//                        string moniker = string.Format("dts://Scripts/{0}/{0}.dll", this.VsaProjectName);
//                        lock (m_assemblyCache)
//                        {
//                            Variables variables = null;
//                            variableDispenser.LockOneForRead("VersionGUID", ref variables);
//                            string str2 = variables[0].Value.ToString();
//                            variables.Unlock();
//                            engineAssembly = (Assembly)m_assemblyCache[moniker + str2];
//                            if (engineAssembly == null)
//                            {
//                                byte[] binaryCode = this.CodeProvider.GetBinaryCode(moniker);
//                                try
//                                {
//                                    engineAssembly = Assembly.Load(binaryCode);
//                                }
//                                catch (BadImageFormatException)
//                                {
//                                    if (events != null)
//                                    {
//                                        events.FireWarning(3, null, Localized.VSAKBWarningWorkaround, "", 0);
//                                    }
//                                    long longLength = binaryCode.LongLength;
//                                    byte[] array = new byte[longLength + 2L];
//                                    binaryCode.CopyTo(array, 0);
//                                    array[(int)((IntPtr)longLength)] = 0;
//                                    array[(int)((IntPtr)(longLength + 1L))] = 0;
//                                    engineAssembly = Assembly.Load(array);
//                                }
//                                m_assemblyCache.Add(moniker + str2, engineAssembly);
//                            }
//                        }
//                        object target = engineAssembly.CreateInstance(this.m_scriptProject + "._Startup");
//                        GlobalObjectProvider provider = new GlobalObjectProvider(scriptObjectModel);
//                        target.GetType().InvokeMember("SetTitle", BindingFlags.InvokeMethod, null, target, new object[] { Localized.DefaultMsgBoxTitle });
//                        target.GetType().InvokeMember("SetSite", BindingFlags.InvokeMethod, null, target, new object[] { provider });
//                        target.GetType().InvokeMember("PushVBHostIntoLibrary", BindingFlags.InvokeMethod, null, target, new object[0]);
//                        target.GetType().InvokeMember("Startup", BindingFlags.InvokeMethod, null, target, new object[0]);
//                    }
//                    catch (Exception)
//                    {
//                        if (events != null)
//                        {
//                            if (IntPtr.Size == 4)
//                            {
//                                events.FireWarning(3, null, Localized.VSAKBWarning, "", 0);
//                            }
//                            else
//                            {
//                                events.FireError(3, null, Localized.VSACannotRecompileOn64, "", 0);
//                                return DTSExecResult.Failure;
//                            }
//                        }
//                    }
//                }
//                if (engineAssembly == null)
//                {
//                    try
//                    {
//                        runtime = new ScriptRuntime(this.m_scriptProject, "dts://Scripts/" + this.m_scriptProject, this.ScriptLanguageInfo.InternalName, this.GetInteractiveMode(variableDispenser), events);
//                        runtime.VsaEngine.SetOption("ApplicationBase", this.m_referencePath);
//                        runtime.SetCodeProviderLocal(this.m_codeProvider);
//                        runtime.LoadSource(this.m_scriptProject);
//                        runtime.AddObject("Dts", scriptObjectModel);
//                        runtime.Run();
//                    }
//                    catch (Exception exception)
//                    {
//                        if (events != null)
//                        {
//                            events.FireError(3, null, Localized.VSACannotCompileAndRun(exception.InnerException.Message), "", 0);
//                        }
//                        return DTSExecResult.Failure;
//                    }
//                    engineAssembly = runtime.EngineAssembly;
//                }
//                try
//                {
//                    if (events.FireQueryCancel())
//                    {
//                        return DTSExecResult.Canceled;
//                    }
//                    object obj3 = null;
//                    foreach (Type type in engineAssembly.GetTypes())
//                    {
//                        if (type.IsClass && (type.Name == this.m_scriptEntryPoint))
//                        {
//                            obj3 = Activator.CreateInstance(type);
//                            break;
//                        }
//                    }
//                    if (obj3 == null)
//                    {
//                        if (events != null)
//                        {
//                            events.FireError(1, null, Localized.CouldNotCreateEntryPointClass(this.m_scriptEntryPoint), "", 0);
//                        }
//                        return DTSExecResult.Failure;
//                    }
//                    try
//                    {
//                        obj3.GetType().InvokeMember("Main", BindingFlags.InvokeMethod, null, obj3, new object[0]);
//                        this.m_executionValue = scriptObjectModel.ExecutionValue;
//                    }
//                    catch (TargetInvocationException exception2)
//                    {
//                        if (this.GetInteractiveMode(variableDispenser))
//                        {
//                            new RuntimeErrorForm(exception2.InnerException, this.VsaProjectName).ShowDialog();
//                        }
//                        if (events != null)
//                        {
//                            events.FireError(2, null, Localized.ScriptThrewException(exception2.InnerException.Message), "", 0);
//                        }
//                        return DTSExecResult.Failure;
//                    }
//                }
//                catch (VsaException exception3)
//                {
//                    if (events != null)
//                    {
//                        events.FireError(3, null, Localized.CompileErrorMsg2((int)exception3.ErrorCode), "", 0);
//                    }
//                    return DTSExecResult.Failure;
//                }
//                finally
//                {
//                    if (runtime != null)
//                    {
//                        runtime.Dispose();
//                    }
//                }
//                if ((events != null) && (scriptObjectModel.TaskResult == 1))
//                {
//                    events.FireError(4, null, Localized.ScriptReportedFailure, "", 0);
//                }
//                return (DTSExecResult)scriptObjectModel.TaskResult;
//            }
//            catch (Exception)
//            {
//                if (events != null)
//                {
//                    events.FireError(5, null, Localized.ScriptFilesFailedToLoad, "", 0);
//                }
//                return DTSExecResult.Failure;
//            }
//        }

//        ~ScriptTask()
//        {
//        }

//        private bool GetInteractiveMode(VariableDispenser variableDispenser)
//        {
//            try
//            {
//                Variables variables = null;
//                variableDispenser.LockForRead("System::InteractiveMode");
//                variableDispenser.GetVariables(ref variables);
//                bool flag = (bool)variables["System::InteractiveMode"].Value;
//                variables.Unlock();
//                return flag;
//            }
//            catch (Exception)
//            {
//                return false;
//            }
//        }

//        internal static string GetSqlPath()
//        {
//            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\90"))
//            {
//                if (key == null)
//                {
//                    return null;
//                }
//                return (key.GetValue("VerSpecificRootDir") as string);
//            }
//        }

//        private void Initialize()
//        {
//            this.ScriptLanguageInfo = Languages.GetDefaultLanguageInfo();
//            this.SetUniqueVsaProjectName();
//            this.m_scriptEntryPoint = "ScriptMain";
//            this.m_saveBinaries = true;
//            this.m_codeProvider.Clear();
//            this.m_breakpointList.Clear();
//            if (this.m_bpManager != null)
//            {
//                this.m_bpManager.ClearBreakpointTargets();
//            }
//            this.m_readOnlyVariables = "";
//            this.m_readWriteVariables = "";
//        }

//        public override void InitializeTask(Connections connections, VariableDispenser variableDispenser, IDTSInfoEvents events, IDTSLogging log, EventInfos eventInfos, LogEntryInfos logEntryInfos, ObjectReferenceTracker refTracker)
//        {
//            logEntryInfos.Add(this.LOG_EVENT_NAMES[0], Localized.ScriptTaskLogEntryDesc, DTSLogEntryFrequency.Consistent);
//        }

//        public void LoadFromXML(XmlElement elemProj, IDTSInfoEvents events)
//        {
//            this.Initialize();
//            try
//            {
//                this.VsaProjectName = elemProj.GetAttribute("Name", "");
//                if (elemProj.HasAttribute("EntryPoint", ""))
//                {
//                    this.m_scriptEntryPoint = elemProj.GetAttribute("EntryPoint", "");
//                }
//                this.ScriptLanguage = elemProj.GetAttribute("Language", "");
//                this.m_saveBinaries = Convert.ToBoolean(elemProj.GetAttribute("SaveBinaries", ""));
//                if (elemProj.HasAttribute("ReadOnlyVariables", ""))
//                {
//                    this.m_readOnlyVariables = elemProj.GetAttribute("ReadOnlyVariables", "");
//                }
//                if (elemProj.HasAttribute("ReadWriteVariables", ""))
//                {
//                    this.m_readWriteVariables = elemProj.GetAttribute("ReadWriteVariables", "");
//                }
//                foreach (XmlElement element in elemProj.ChildNodes)
//                {
//                    string attribute = element.GetAttribute("Name", "");
//                    if (((element.NodeType == XmlNodeType.Element) && (element.Name == "ProjectItem")) && (element.FirstChild.NodeType == XmlNodeType.CDATA))
//                    {
//                        XmlCDataSection firstChild = (XmlCDataSection)element.FirstChild;
//                        this.m_codeProvider.SourceCodeTable.Add(attribute, firstChild.Data);
//                        continue;
//                    }
//                    if ((element.NodeType == XmlNodeType.Element) && (element.Name == "BinaryItem"))
//                    {
//                        this.m_codeProvider.BinaryCodeTable[attribute] = Convert.FromBase64String(element.InnerXml);
//                        continue;
//                    }
//                    if ((element.NodeType == XmlNodeType.Element) && (element.Name == "Breakpoint"))
//                    {
//                        ScriptBreakpointInfo info = new ScriptBreakpointInfo();
//                        info.Name = element.GetAttribute("Name", "");
//                        info.Enabled = Convert.ToBoolean(element.GetAttribute("Enabled", ""));
//                        info.Function = element.GetAttribute("Function", "");
//                        info.File = element.GetAttribute("File", "");
//                        info.FileLine = Convert.ToInt32(element.GetAttribute("Line", ""));
//                        info.FileColumn = Convert.ToInt32(element.GetAttribute("Column", ""));
//                        info.Condition = element.GetAttribute("Condition", "");
//                        info.ConditionType = Convert.ToInt32(element.GetAttribute("ConditionType", ""));
//                        info.Language = element.GetAttribute("Language", "");
//                        info.HitCount = Convert.ToInt32(element.GetAttribute("HitCount", ""));
//                        info.HitCountType = Convert.ToInt32(element.GetAttribute("HitCountType", ""));
//                        this.m_breakpointList.Add(info);
//                    }
//                }
//                this.ReportBreakpointChanges();
//            }
//            catch (Exception exception)
//            {
//                if (events != null)
//                {
//                    events.FireError(6, null, Localized.LoadFromXMLException(exception.ToString()), "", 0);
//                }
//                this.Initialize();
//            }
//        }

//        public void ReportBreakpointChanges()
//        {
//            this.m_bpManager.ClearBreakpointTargets();
//            for (int i = 0; i < this.m_breakpointList.Count; i++)
//            {
//                ScriptBreakpointInfo info = (ScriptBreakpointInfo)this.m_breakpointList[i];
//                this.m_bpManager.CreateBreakpointTarget(i, info.Name).Enabled = info.Enabled;
//            }
//        }

//        public void ResumeExecution()
//        {
//        }

//        public void SaveToXML(XmlDocument doc, IDTSInfoEvents events)
//        {
//            XmlElement newChild = doc.CreateElement(null, "ScriptProject", null);
//            doc.AppendChild(newChild);
//            newChild.SetAttribute("Name", null, this.m_scriptProject);
//            newChild.SetAttribute("Language", null, this.ScriptLanguageInfo.DisplayName);
//            newChild.SetAttribute("EntryPoint", null, this.m_scriptEntryPoint);
//            newChild.SetAttribute("SaveBinaries", null, this.m_saveBinaries.ToString());
//            newChild.SetAttribute("ReadOnlyVariables", null, this.m_readOnlyVariables);
//            newChild.SetAttribute("ReadWriteVariables", null, this.m_readWriteVariables);
//            foreach (string str in this.m_codeProvider.SourceCodeTable.Keys)
//            {
//                XmlElement element2 = doc.CreateElement(null, "ProjectItem", null);
//                newChild.AppendChild(element2);
//                char[] chArray2 = new char[3];
//                chArray2[0] = '\r';
//                chArray2[1] = '\n';
//                char[] trimChars = chArray2;
//                string str2 = ((string)this.m_codeProvider.SourceCodeTable[str]).TrimEnd(trimChars);
//                element2.InnerXml = "\n<![CDATA[" + str2 + "]]>";
//                element2.SetAttribute("Name", null, str);
//            }
//            if (this.m_saveBinaries)
//            {
//                foreach (string str3 in this.m_codeProvider.BinaryCodeTable.Keys)
//                {
//                    XmlElement element3 = doc.CreateElement(null, "BinaryItem", null);
//                    newChild.AppendChild(element3);
//                    element3.InnerXml = Convert.ToBase64String((byte[])this.m_codeProvider.BinaryCodeTable[str3], Base64FormattingOptions.InsertLineBreaks);
//                    element3.SetAttribute("Name", null, str3);
//                }
//            }
//            foreach (ScriptBreakpointInfo info in this.m_breakpointList)
//            {
//                XmlElement element4 = doc.CreateElement(null, "Breakpoint", null);
//                newChild.AppendChild(element4);
//                element4.SetAttribute("Name", null, info.Name);
//                element4.SetAttribute("Enabled", null, info.Enabled.ToString());
//                element4.SetAttribute("Function", null, info.Function);
//                element4.SetAttribute("File", null, info.File);
//                element4.SetAttribute("Line", null, info.FileLine.ToString());
//                element4.SetAttribute("Column", null, info.FileColumn.ToString());
//                element4.SetAttribute("Condition", null, info.Condition);
//                element4.SetAttribute("ConditionType", null, info.ConditionType.ToString());
//                element4.SetAttribute("Language", null, info.Language);
//                element4.SetAttribute("HitCount", null, info.HitCount.ToString());
//                element4.SetAttribute("HitCountType", null, info.HitCountType.ToString());
//            }
//        }

//        public void SetUniqueVsaProjectName()
//        {
//            this.VsaProjectName = "ScriptTask_" + Guid.NewGuid().ToString("N");
//        }

//        public void SuspendExecution()
//        {
//        }

//        public override DTSExecResult Validate(Connections connections, VariableDispenser variableDispenser, IDTSComponentEvents events, IDTSLogging log)
//        {
//            DTSExecResult success = DTSExecResult.Success;
//            if (this.ScriptLoaded)
//            {
//                if (this.PreCompile && !this.CodeProvider.HasBinaryCode)
//                {
//                    events.FireError(8, null, Localized.BinaryCodeNotFound, "", 0);
//                }
//                if (!this.PreCompile && (IntPtr.Size == 8))
//                {
//                    events.FireError(9, null, Localized.NotPrecompiledOn64, "", 0);
//                    success = DTSExecResult.Failure;
//                }
//            }
//            return success;
//        }

//        [Browsable(false)]
//        public BreakpointManager BreakpointManager
//        {
//            get
//            {
//                return this.m_bpManager;
//            }
//        }

//        [Browsable(false)]
//        public ArrayList Breakpoints
//        {
//            get
//            {
//                return this.m_breakpointList;
//            }
//        }

//        [Browsable(false)]
//        public ScriptTaskCodeProvider CodeProvider
//        {
//            get
//            {
//                return this.m_codeProvider;
//            }
//        }

//        public bool DebugMode
//        {
//            get
//            {
//                return (this.m_debugMode != 0);
//            }
//            set
//            {
//                Interlocked.Exchange(ref this.m_debugMode, value ? 1 : 0);
//            }
//        }

//        public string EntryPoint
//        {
//            get
//            {
//                return this.m_scriptEntryPoint;
//            }
//            set
//            {
//                this.m_scriptEntryPoint = value;
//            }
//        }

//        public override object ExecutionValue
//        {
//            get
//            {
//                return this.m_executionValue;
//            }
//        }

//        public bool PreCompile
//        {
//            get
//            {
//                return this.m_saveBinaries;
//            }
//            set
//            {
//                if (value != this.m_saveBinaries)
//                {
//                    this.CodeProvider.ClearBinaryCode();
//                    this.m_saveBinaries = value;
//                }
//            }
//        }

//        public string ReadOnlyVariables
//        {
//            get
//            {
//                return this.m_readOnlyVariables;
//            }
//            set
//            {
//                this.m_readOnlyVariables = value;
//            }
//        }

//        public string ReadWriteVariables
//        {
//            get
//            {
//                return this.m_readWriteVariables;
//            }
//            set
//            {
//                this.m_readWriteVariables = value;
//            }
//        }

//        public string ScriptLanguage
//        {
//            get
//            {
//                return this.ScriptLanguageInfo.DisplayName;
//            }
//            set
//            {
//                this.ScriptLanguageInfo = Languages.GetLanguageInfo(value);
//            }
//        }

//        [Browsable(false)]
//        public bool ScriptLoaded
//        {
//            get
//            {
//                if (this.m_codeProvider.SourceCodeTable.Count == 0)
//                {
//                    return (this.m_codeProvider.BinaryCodeTable.Count != 0);
//                }
//                return true;
//            }
//        }

//        public bool SuspendRequired
//        {
//            get
//            {
//                return false;
//            }
//            set
//            {
//            }
//        }

//        [Browsable(false)]
//        public string VsaProjectName
//        {
//            get
//            {
//                return this.m_scriptProject;
//            }
//            set
//            {
//                if (this.ActiveProject == null)
//                {
//                    lock (base.GetType())
//                    {
//                        this.m_codeProvider.RenameAll(this.m_scriptProject, value);
//                        goto Label_0048;
//                    }
//                }
//                try
//                {
//                    this.ActiveProject.Name = value;
//                }
//                catch (Exception)
//                {
//                    this.ActiveProject = null;
//                }
//            Label_0048:
//                this.m_scriptProject = value;
//            }
//        }

//        private enum ENUMLOG
//        {
//            LogEntry
//        }
//    }



//}
